/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mediana;

import java.util.ArrayList;
import java.util.Random;
import org.omg.CORBA.BAD_PARAM;

/**
 *
 * @author mateusz.ledzewicz
 */
public class Table {
    public int table[][];
   
    public Table(int size) throws Exception{
        if(size % 2 != 1)
            throw new BAD_PARAM();
        this.table = new int[size][size];
        this.generateNumbers(Mediana.RandMax);
    }
    
    public Table(Table obj){
        int size = obj.table.length;
        this.table = new int[size][size];
         for(int i=0;i<obj.getSize(); i++){
            this.setElement(i, obj.getElement(i));
        }
    }
    
    public void generateNumbers(int max){
        Random rnd = new Random();
        for(int i = 0; i < this.getSize(); i++){
            this.setElement(i, rnd.nextInt(max));
        }
    }
    
    public int getSize(){
        return this.table.length*this.table.length;
    }
    
    public int getElement(int index){
        int row = index/this.table.length;
        int col = index%this.table.length;
        return this.table[row][col];
    }
    
    public void setElement(int index, int value){
        int row = index/this.table.length;
        int col = index%this.table.length;
        this.table[row][col] = value;
    }
    
    public int getMedian(){
        int index = (this.table.length - 1)/2;
        return this.table[index][index];
    }
    
    @Override
    public String toString(){
        String ret = "";
        for(int i=0; i<this.table.length;i++){
           for(int j=0; j<this.table.length;j++){
               ret = ret + " " + this.table[i][j];
           } 
           ret = ret + "\n";
        }
        return ret;
        
    }
    
    public boolean isSorted(){
        for(int i=0;i<this.getSize()-1; i++){
            if(this.getElement(i) > this.getElement(i+1))
                return false;
        }
        return true;
    }
    
     public void swap(int index1, int index2){
        int temp = this.getElement(index1);
        this.setElement(index1, this.getElement(index2));
        this.setElement(index2, temp);
     
    }
    
    
}
